{% extends "UiManager/base.html" %}
{% block title %}
    页面管理
{% endblock %}
{% block new %}
    <button class="btn btn-danger btn-sm fa fa-plus" style="border-radius: 20px" data-toggle="modal" data-target="#newProject">&nbsp;新建页面</button>
{% endblock %}
{% block location %}
    <div class="am-icon-home fa fa-home" style="font-weight: bold;color: grey; float: right;">&nbsp;当前位置： 页面管理 > 页面列表</div>
{% endblock %}
{% load staticfiles %}
{% block css %}
{% endblock %}
{% block js %}
    <script>

    function seleList() {
        $("#newSel,#newSel1,#name_xm").find('option').remove();
        $("#name_xm").append('<option>所属项目</option>');
        var datas = {
            "pageSize": 99999
        }
        var data = JSON.stringify(datas);
        $.ajax({
            url: '/api/v1/project?p=1',
            type: 'post',
            dataType: 'json',
            data: data,
            success: function (res) {
                for (var i = 0; i < res.data.projects.length; i++) {
                    $("#newSel,#newSel1,#name_xm").append('<option ID="' + res.data.projects[i].id + '" value="' + res.data.projects[i].name + '">' + res.data.projects[i].name + '</option>');
                }
            }
        });
    }

    /*编辑页面*/
    function edit(id) {
        $('#edit_page').modal('show');
        var data = {
            id: id,
            type: 'edit'
        };
        $.ajax({
            url: '/ui/edit_page/',
            type: 'post',
            data: JSON.stringify(data),
            dataType: 'json',
            success: function (res) {
                $("#page_name").val(res.page_name);        //页面名称
                $("#edit_project_name").append("<option>"+res.belong_project+"</option>");
                $("#simple_desc").val(res.simple_desc);
                $("#edit-save").attr("onclick", "editSave(" + id + ")");
            }
        });
    }

    function editSave(id) {
        var page_name, simple_desc;
        page_name = $("#page_name").val();
        simple_desc = $("#simple_desc").val();
        var data = JSON.stringify({'id':id, "type":'edit_save', "page_name": page_name, "simple_desc": simple_desc});
        $.ajax({
            url: '/ui/edit_page/',
            type: 'post',
            contentType: 'application/json',
            data: data,
            dataType: 'json',
            success: function (res) {
                if (res.code == 0) {
                    $("#edit_page").modal("hide");
                    location.reload();
                } else {
                    $(".js_BJmessage").html(res.message);
                }
            }
        });
    }



    /*删除询问方法*/
    function isDel(id) {
        $("#delButton").attr("onclick", "del(" + id + ")");
        $("#deleteObj").modal("show");
    }

    /*删除方法*/
    function del(id) {
        var data = {
          "id": id,
          "type": "delete"
        };
        $.ajax({
            url: '/ui/edit_page/',
            type: 'post',
            contentType: 'application/json',
            dataType: 'json',
            data:JSON.stringify(data),
            success: function (res) {
                if (res.code == 0) {
                    $("#deleteObj").modal("hide");
                    location.reload();
                } else {
                    $("#delMessage").html(res.message);
                }
            }
        });
    }

    /*新建方法*/
    function NewList() {
        var newName, newSel, newText;
        newName = $("#newName").val();//页面名称
        newSel = $("#newSel option:selected").val();//所属项目
        newText = $("#newText").val();//备注信息
        var datas = {
            "type": "add",
            "belong_project": newSel,
            "page_name": newName,
            "simple_desc": newText,
        };
        var data = JSON.stringify(datas);
        $.ajax({
            url: '/ui/add_page/',
            type: 'post',
            contentType: 'application/json',
            data: data,
            dataType: 'json',
            success: function (res) {
                if (res.code == 0) {
                    //重新加载页面
                    location.reload()
                    //取消方法
                    closeDown()
                } else {
                    $(".js_message").html(res.message);
                }
            }
        });
    }

    /*新建弹窗取消*/
    function closeDown() {
        $('#newProject').modal('hide');
        $("#delMessage").html("");
        $("#newName").val("");             //页面名称
        $("#newSel option:first").prop("selected", 'selected');  //所属项目
        $("#newText").val("");           //备注信息
        $("#deleteObj").modal('hide')           //删除提示信息
    }

</script>
{% endblock %}
{% block content %}
    <div id="page-wrapper">
        <div id="page-inner">
            <!-- /. ROW  -->
            <!-- /. 表格  -->
            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <!--<div class="panel-heading">表格名字</div>-->
                        <div class="panel-body" style="min-height:500px">
                            <div class="row mar-bottom-20">
                                <div class="form-inline">
                                    <form id="pro_filter" method="POST" action="/ui/page_list/">
                                        <div class="col-lg-12">
                                            <div class="input-group">
                                                <select class="form-control selectpicker" data-style="btn-danger" name="select_project_name">
                                                    <option value="0">全部项目</option>
                                                    {% for project in project_list %}
                                                        {% if  project.id == select_project_name %}
                                                            <option value="{{ project.id }}" selected="selected">{{ project.project_name }}</option>
                                                        {% else %}
                                                            <option value="{{ project.id }}">{{ project.project_name }}</option>
                                                        {% endif %}
                                                    {% endfor %}
                                                </select>
                                            </div>
                                            <div class="input-group">
                                              <input name="search_page_name" type="text" class="form-control" placeholder="页面名称" value="{{ search_page_name }}">
                                                <span class="input-group-btn">
                                                <button class="btn btn-primary" type="submit">搜索</button>
                                              </span>
                                            </div><!-- /input-group -->
                                        </div><!-- /.col-lg-12 -->
                                    </form>
                                </div>
                            </div>
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th>页面名称</th>
                                        <th>所属项目</th>
                                        <th>创建时间</th>
                                        <th>更新时间</th>
                                        <th>描述信息</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody class="js_table">
                                    {% for row in obj %}
                                        <tr>
                                            <td>{{ row.id }}</td>
                                            <td>{{ row.page_name }}</td>
                                            <td>{{ row.belong_project.project_name }}</td>
                                            <td>{{ row.create_time }}</td>
                                            <td>{{ row.update_time }}</td>
                                            <td>{{ row.simple_desc }}</td>
                                            <td>
                                                <button class="btn-primary" onclick='edit("{{ row.id }}")'>编辑</button>
                                                <button class="btn-danger" onclick="isDel('{{ row.id }}')">删除</button>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                            <ul class="pagination" id="pager">
                                {#上一页按钮开始#}
                                {# 如果当前页有上一页#}
                                {% if obj.has_previous %}
                                    {#  当前页的上一页按钮正常使用#}
                                    <li class="previous"><a href="/ui/page_list?page={{ obj.previous_page_number }}">&laquo;</a></li>
                                {% else %}
                                    {# 当前页的不存在上一页时,上一页的按钮不可用#}
                                    <li class="previous disabled"><a href="#">&laquo;</a></li>
                                {% endif %}
                                {#上一页按钮结束#}
                                {# 页码开始#}
                                {% for num in paginator.page_range %}

                                    {% if num == currentPage %}
                                        <li class="item active"><a href="/ui/page_list?page={{ num }}">{{ num }}</a></li>
                                    {% else %}
                                        <li class="item"><a href="/ui/page_list?page={{ num }}">{{ num }}</a></li>
                                    {% endif %}
                                {% endfor %}
                                {#页码结束#}
                                {# 下一页按钮开始#}
                                {% if obj.has_next %}
                                    <li class="next"><a href="/ui/page_list?page={{ obj.next_page_number }}">&raquo;</a></li>
                                {% else %}
                                    <li class="next disabled"><a href="#">&raquo;</a></li>
                                {% endif %}
                                {# 下一页按钮结束#}
                            </ul>
                        </div>
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>
            <!-- /. 表格  -->
        </div>
        <!-- /. PAGE INNER  -->
    </div>
    <!-- /. PAGE WRAPPER  -->
</div>

<!--新建弹窗-->
<div class="modal fade" id="newProject" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">新建页面</h4>
            </div>
            <div class="modal-body">
                <!--内容-->
                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="newName" class="col-sm-2 control-label">页面名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="newName" maxlength="20" placeholder="20个字符以内">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="lastname" class="col-sm-2 control-label">所属项目</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="newSel">
                                {% for project in project_list %}
                                    <option>{{ project.project_name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="lastname" class="col-sm-2 control-label">备注信息</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" id="newText" rows="3"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="lastname" class="col-sm-2 control-label"></label>
                        <div class="col-sm-10 js_message text-danger"></div>
                    </div>


                </form>
                <!--内容-->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" onclick="closeDown()">取消</button>
                <button type="button" class="btn btn-primary" onclick="NewList()">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<!--end 新建弹窗-->
<!--编辑弹窗-->
<div class="modal fade" id="edit_page" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel1">编辑页面</h4>
            </div>
            <div class="modal-body">
                <!--内容-->
                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="page_name" class="col-sm-2 control-label">页面名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="page_name" maxlength="20" placeholder="50个字符以内">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edit_project_name" class="col-sm-2 control-label">所属项目</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="edit_project_name" disabled="disabled">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="simple_desc" class="col-sm-2 control-label">备注信息</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" id="simple_desc" rows="3"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="lastname" class="col-sm-2 control-label"></label>
                        <div class="col-sm-10 js_BJmessage text-danger"></div>
                    </div>
                </form>
                <!--内容-->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="edit-save">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<!--end 编辑弹窗-->
<!--删除询问弹窗提示-->
<div class="modal fade" id="deleteObj" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title">删除页面</h4>
            </div>
            <div class="modal-body">
                <p>请确认是否删除页面</p>
                <p id='delMessage' class="js_DELmessage text-danger"></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" onclick="closeDown()">取消</button>
                <button type="button" class="btn btn-primary" id="delButton">确定</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<!--end 删除询问弹窗提示-->
{% endblock %}